package com.dyit.controllor;

import com.dyit.dto.HttpResp;
import com.dyit.entity.Customer;
import com.dyit.service.ICustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/api/customer")
public class CustomerController {
    @Autowired
    ICustomerService ics;

    @GetMapping("/findCustomer")
    public HttpResp findCustomerByPhoneAndPassword(@RequestParam("phone") String phone,@RequestParam("password") String password){
        if (ics.findCustomerByPhoneAndPassword(phone,password) != null){
            if (ics.findCustomerByPhoneAndPassword(phone,password).getRole() == 1){
                return new HttpResp(2001,"登录成功",ics.findCustomerByPhoneAndPassword(phone,password), LocalDateTime.now());
            }else {
                return new HttpResp(2002,"登录成功",ics.findCustomerByPhoneAndPassword(phone,password), LocalDateTime.now());
            }
        }else{
            return new HttpResp(2000,"登陆失败，请检查您的用户名或密码",null,LocalDateTime.now());
        }

    }

    @GetMapping("/findAll")
    public HttpResp findAll(){
        if (ics.findAll() != null){
            return new HttpResp(2003,"查询成功",ics.findAll(),LocalDateTime.now());
        }else {
            return new HttpResp(2000,"查询失败，请检查您的后台代码",null,LocalDateTime.now());
        }
    }

    @PostMapping("/enroll")
    public HttpResp enroll(@RequestBody Customer customer){
        if (customer!=null){
            ics.add(customer);
            return new HttpResp(
                    2001,
                    "注册成功",
                    null,
                    LocalDateTime.now()
            );
        }
        return new HttpResp(
                2001,
                "注册失败，请重试",
                null,
                LocalDateTime.now()
        );
    }

    @PutMapping("/update")
    public HttpResp update(@RequestBody Customer customer){
        if (customer!=null){
            ics.update(customer);
            return new HttpResp(
                    2001,
                    "修改成功",
                    null,
                    LocalDateTime.now()
            );
        }
        return new HttpResp(
                2002,
                "修改失败",
                null,
                LocalDateTime.now()
        );
    }
}
